package chamster.statemachine;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import chamster.MaeanderHamster;
import chamster.maeander.ForewardState;
import chamster.maeander.MaeanderState;
import chamster.maeander.RandomState;

public class MaeanderDEA implements StateMachine<MaeanderHamster> {

	private final double randomChance;
	private final int randomSpan;
	
	public MaeanderDEA(double randomChance, int randomSpan) {
		this.randomChance = randomChance;
		this.randomSpan = randomSpan;
	}
	
	public HamsterState<MaeanderHamster> run(MaeanderHamster hamster,
			HamsterState<MaeanderHamster> state) throws Exception {
		List<HamsterState<MaeanderHamster>> newStates = 
				state.applyState(new LinkedList<HamsterState<MaeanderHamster>>(), hamster);
		if((Math.random() * this.randomChance) > 1 
				&& !(state instanceof RandomState)) {
			return new RandomState(this.randomSpan, (MaeanderState)state);
		}
		if(hamster.getVisitHistory().all(true)
				&& !(state instanceof RandomState)) {
			return new RandomState(5, new ForewardState());
		}
		if(newStates.isEmpty()) {
			newStates.add(new ForewardState());
		}
		return newStates.get(0);
	}

}
